package com.javalearn;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

public class SynAtomicCompare {
	private  int c1=0;
	private  AtomicInteger c2 = new AtomicInteger(0);
	public void synAdd() {
		synchronized(SynAtomicCompare.class){
			c1++;
		}
	}
	public void aicAdd() {
		c2.getAndAdd(1);
	}
	public void print() {
		System.out.println("c1:"+c1+"c2:"+c2);
	}
	public  class MyThread extends Thread{
		public void run() {
			for(int i=0;i<10000;i++)
				//synAdd();//测试synchronized
			    aicAdd();//测试AtomicInteger
		}
	}
	public static void main(String args[]) {
		SynAtomicCompare t = new SynAtomicCompare();
		List<MyThread> list = new ArrayList<MyThread>();
		int threadNum = 10000;
		for(int i=0;i<threadNum;i++) {
			MyThread myThread = t.new MyThread();
			list.add(myThread);
		}
		long start = System.currentTimeMillis();
		for(int i=0;i<threadNum;i++) {
			list.get(i).start();
		}

		for(int i=0;i<threadNum;i++) {
			try {
				list.get(i).join();
			}catch(InterruptedException e) {
				e.printStackTrace();
			}
		}
		long end = System.currentTimeMillis();
		t.print();
		long delta = end - start;
		System.out.println("spend time :"+delta);
	}}
